home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / nos042_s / icmp.c < prev    next >
C/C++ Source or Header  |  1994-09-16  |  7KB  |  271 lines

  1. /* Internet Control Message Protocol (ICMP)
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4.  
  5. /****************************************************************************
  6. *    $Id: icmp.c 1.2 93/07/16 11:45:21 ROOT_DOS Exp $
  7. *    14 Jun 93    1.2        GT    Fix warnings.                                    *
  8. ****************************************************************************/
  9.  
  10. #include <stdio.h>
  11. #include "global.h"
  12. #include "mbuf.h"
  13. #include "iface.h"
  14. #include "ip.h"
  15. #include "icmp.h"
  16. #include "netuser.h"
  17.  
  18. struct mib_entry Icmp_mib[] = {
  19.     "",            0,
  20.     "icmpInMsgs",        0,
  21.     "icmpInErrors",        0,
  22.     "icmpInDestUnreachs",    0,
  23.     "icmpInTimeExcds",    0,
  24.     "icmpInParmProbs",    0,
  25.     "icmpInSrcQuenchs",    0,
  26.     "icmpInRedirects",    0,
  27.     "icmpInEchos",        0,
  28.     "icmpInEchoReps",    0,
  29.     "icmpInTimestamps",    0,
  30.     "icmpInTimestampReps",    0,
  31.     "icmpInAddrMasks",    0,
  32.     "icmpInAddrMaskReps",    0,
  33.     "icmpOutMsgs",        0,
  34.     "icmpOutErrors",    0,
  35.     "icmpOutDestUnreachs",    0,
  36.     "icmpOutTimeExcds",    0,
  37.     "icmpOutParmProbs",    0,
  38.     "icmpOutSrcQuenchs",    0,
  39.     "icmpOutRedirects",    0,
  40.     "icmpOutEchos",        0,
  41.     "icmpOutEchoReps",    0,
  42.     "icmpOutTimestamps",    0,
  43.     "icmpOutTimestampReps",    0,
  44.     "icmpOutAddrMasks",    0,
  45.     "icmpOutAddrMaskReps",    0,
  46. };
  47.  
  48. /* Process an incoming ICMP packet */
  49. void
  50. icmp_input(iface,ip,bp,rxbroadcast)
  51. struct iface *iface;    /* Incoming interface (ignored) */
  52. struct ip *ip;        /* Pointer to decoded IP header structure */
  53. struct mbuf *bp;    /* Pointer to ICMP message */
  54. int rxbroadcast;
  55. {
  56.     struct icmplink *ipp;
  57.     struct mbuf *tbp;
  58.     struct icmp icmp;    /* ICMP header */
  59.     struct ip oip;        /* Offending datagram header */
  60.     int16 type;        /* Type of ICMP message */
  61.     int16 length;
  62.  
  63.     icmpInMsgs++;
  64.     if(rxbroadcast){
  65.         /* Broadcast ICMP packets are to be IGNORED !! */
  66.         icmpInErrors++;
  67.         free_p(bp);
  68.         return;
  69.     }
  70.     length = ip->length - IPLEN - ip->optlen;
  71.     if(cksum(NULLHEADER,bp,length) != 0){
  72.         /* Bad ICMP checksum; discard */
  73.         icmpInErrors++;
  74.         free_p(bp);
  75.         return;
  76.     }
  77.     ntohicmp(&icmp,&bp);
  78.  
  79.     /* Process the message. Some messages are passed up to the protocol
  80.      * module for handling, others are handled here.
  81.      */
  82.     type = icmp.type;
  83.     
  84.     switch(uchar(type)){
  85.     case ICMP_TIME_EXCEED:    /* Time-to-live Exceeded */
  86.     case ICMP_DEST_UNREACH:    /* Destination Unreachable */
  87.     case ICMP_QUENCH:    /* Source Quench */
  88.         switch(uchar(type)){
  89.         case ICMP_TIME_EXCEED:    /* Time-to-live Exceeded */
  90.             icmpInTimeExcds++;
  91.             break;
  92.         case ICMP_DEST_UNREACH:    /* Destination Unreachable */
  93.             icmpInDestUnreachs++;
  94.             break;
  95.         case ICMP_QUENCH:    /* Source Quench */
  96.             icmpInSrcQuenchs++;
  97.             break;
  98.         }
  99.         ntohip(&oip,&bp);    /* Extract offending IP header */
  100.         if(Icmp_trace){
  101.             printf("ICMP from %s:",inet_ntoa(ip->source));
  102.             printf(" dest %s %s",inet_ntoa(oip.dest),
  103.              smsg(Icmptypes,ICMP_TYPES,uchar(type)));
  104.             switch(uchar(type)){
  105.             case ICMP_TIME_EXCEED:
  106.                 printf(" %s\n",
  107.                  smsg(Exceed,NEXCEED,uchar(icmp.code)));
  108.                 break;
  109.             case ICMP_DEST_UNREACH:
  110.                 printf(" %s\n",
  111.                  smsg(Unreach,NUNREACH,uchar(icmp.code)));
  112.                 break;
  113.             default:
  114.                 printf(" %u\n",uchar(icmp.code));
  115.                 break;
  116.             }
  117.         }
  118.         for(ipp = Icmplink;ipp->funct != NULL;ipp++)
  119.             if(ipp->proto == oip.protocol)
  120.                 break;
  121.         if(ipp->funct != NULL){
  122.             (*ipp->funct)(ip->source,oip.source,oip.dest,icmp.type,
  123.              icmp.code,&bp);
  124.         }
  125.         break;
  126.     case ICMP_ECHO:        /* Echo Request */
  127.         /* Change type to ECHO_REPLY, recompute checksum,
  128.          * and return datagram.
  129.          */
  130.         icmpInEchos++;
  131.         icmp.type = ICMP_ECHO_REPLY;
  132.         if((tbp = htonicmp(&icmp,bp)) == NULLBUF){
  133.             free_p(bp);
  134.             return;
  135.         }
  136.         icmpOutEchoReps++;
  137.         ip_send(ip->dest,ip->source,ICMP_PTCL,ip->tos,0,tbp,length,0,0);
  138.         return;
  139.     case ICMP_REDIRECT:    /* Redirect */
  140.         icmpInRedirects++;
  141.         break;
  142.     case ICMP_PARAM_PROB:    /* Parameter Problem */
  143.         icmpInParmProbs++;
  144.         break;
  145.     case ICMP_ECHO_REPLY:    /* Echo Reply */
  146.         icmpInEchoReps++;
  147.         echo_proc(ip->source,ip->dest,&icmp,bp);
  148.         bp = NULLBUF;    /* so it won't get freed */
  149.         break;
  150.     case ICMP_TIMESTAMP:    /* Timestamp */
  151.         icmpInTimestamps++;
  152.         break;
  153.     case ICMP_TIME_REPLY:    /* Timestamp Reply */
  154.         icmpInTimestampReps++;
  155.         break;
  156.     case ICMP_INFO_RQST:    /* Information Request */
  157.         break;
  158.     case ICMP_INFO_REPLY:    /* Information Reply */
  159.         break;
  160.     }
  161.     free_p(bp);
  162. }
  163. /* Return an ICMP response to the sender of a datagram.
  164.  * Unlike most routines, the callER frees the mbuf.
  165.  */
  166. int
  167. icmp_output(
  168.     struct ip *ip,                /* Header of offending datagram     */
  169.     struct mbuf *data,        /* Data portion of datagram         */
  170.     char type,                    /* Codes to send                        */ 
  171.     char code,
  172.     union icmp_args *args)
  173. {
  174.     struct mbuf *bp = NULLBUF;
  175.     struct icmp icmp;    /* ICMP protocol header */
  176.     int16 dlen;        /* Length of data portion of offending pkt */
  177.     int16 length;        /* Total length of reply */
  178.  
  179.     if(ip == NULLIP)
  180.         return -1;
  181.     if(uchar(ip->protocol) == ICMP_PTCL){
  182.         /* Peek at type field of ICMP header to see if it's safe to
  183.          * return an ICMP message
  184.          */
  185.         switch(uchar(data->data[0])){
  186.         case ICMP_ECHO_REPLY:
  187.         case ICMP_ECHO:
  188.         case ICMP_TIMESTAMP:
  189.         case ICMP_TIME_REPLY:
  190.         case ICMP_INFO_RQST:
  191.         case ICMP_INFO_REPLY:
  192.             break;    /* These are all safe */
  193.         default:
  194.             /* Never send an ICMP error message about another
  195.              * ICMP error message!
  196.              */
  197.             return -1;
  198.         }
  199.     }
  200.     /* Compute amount of original datagram to return.
  201.      * We return the original IP header, and up to 8 bytes past that.
  202.      */
  203.     dlen = min(8,len_p(data));
  204.     length = dlen + ICMPLEN + IPLEN + ip->optlen;
  205.     /* Take excerpt from data portion */
  206.     if(data != NULLBUF && dup_p(&bp,data,0,dlen) == 0)
  207.         return -1;    /* The caller will free data */
  208.  
  209.     /* Recreate and tack on offending IP header */
  210.     if((data = htonip(ip,bp,IP_CS_NEW)) == NULLBUF){
  211.         free_p(bp);
  212.         icmpOutErrors++;
  213.         return -1;
  214.     }
  215.     icmp.type = type;
  216.     icmp.code = code;
  217.     icmp.args.unused = 0;
  218.     switch(uchar(icmp.type)){
  219.     case ICMP_PARAM_PROB:
  220.         icmpOutParmProbs++;
  221.         icmp.args.pointer = args->pointer;
  222.         break;
  223.     case ICMP_REDIRECT:
  224.         icmpOutRedirects++;
  225.         icmp.args.address = args->address;
  226.         break;
  227.     case ICMP_ECHO:
  228.         icmpOutEchos++;
  229.         break;
  230.     case ICMP_ECHO_REPLY:
  231.         icmpOutEchoReps++;
  232.         break;
  233.     case ICMP_INFO_RQST:
  234.         break;
  235.     case ICMP_INFO_REPLY:
  236.         break;
  237.     case ICMP_TIMESTAMP:
  238.         icmpOutTimestamps++;
  239.         break;
  240.     case ICMP_TIME_REPLY:
  241.         icmpOutTimestampReps++;
  242.         icmp.args.echo.id = args->echo.id;
  243.         icmp.args.echo.seq = args->echo.seq;
  244.         break;
  245.     case ICMP_ADDR_MASK:
  246.         icmpOutAddrMasks++;
  247.         break;
  248.     case ICMP_ADDR_MASK_REPLY:
  249.         icmpOutAddrMaskReps++;
  250.         break;
  251.     case ICMP_DEST_UNREACH:
  252.         if(icmp.code == ICMP_FRAG_NEEDED)
  253.             icmp.args.mtu = args->mtu;
  254.         icmpOutDestUnreachs++;
  255.         break;
  256.     case ICMP_TIME_EXCEED:
  257.         icmpOutTimeExcds++;
  258.         break;
  259.     case ICMP_QUENCH:
  260.         icmpOutSrcQuenchs++;
  261.         break;
  262.     }
  263.     icmpOutMsgs++;
  264.     /* Now stick on the ICMP header */
  265.     if((bp = htonicmp(&icmp,data)) == NULLBUF){
  266.         free_p(data);
  267.         return -1;
  268.     }
  269.     return ip_send(INADDR_ANY,ip->source,ICMP_PTCL,ip->tos,0,bp,length,0,0);
  270. }
  271.